﻿using Newtonsoft.Json;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using NPOI.SS.Formula.Atp;
using OSSUtility;
using OSSUtility.BLL;
using OSSUtility.Models;
using Ozone.DAL;
using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.BLL
{
    public class SetRobotPointsBLL
    {
        public static RobotPointNameModel PointName = new RobotPointNameModel();
        //查询
        public static List<robot_points> QureyRP(DateTime Starttime, DateTime EndTime)
        {
            return RobotPointsDAL.QueryRobotPoint("", Starttime, EndTime);
        }
        public static List<robot_points> QureyRP(string sPoName)
        {
            return RobotPointsDAL.QueryRobotPoint(sPoName);
        }

        //插入
        public static bool AddRP(robot_points robot_p)
        {
            List<robot_points> lstrobot_p = new List<robot_points>();
            lstrobot_p.Add(robot_p);
            return RobotPointsDAL.AddRobotPoint(lstrobot_p);
        }
        public static bool AddRP(List<robot_points> lstrobot_p)
        {
            return RobotPointsDAL.AddRobotPoint(lstrobot_p);
        }

        //删除
        public static bool DeleteRP(string sPoName)
        {
            List<string> lstPoName = new List<string>();
            lstPoName.Add(sPoName);
            return RobotPointsDAL.DeleteRobotPoint(lstPoName);
        }
        public static bool DeleteRP(List<string> lstPoName)
        {
            return RobotPointsDAL.DeleteRobotPoint(lstPoName);
        }

        //更改
        public static bool UpdateRP(robot_points robot_p)
        {
            List<robot_points> lstrobot_p = new List<robot_points>();
            lstrobot_p.Add(robot_p);
            return RobotPointsDAL.UpdateRobotPoint(lstrobot_p);
        }
        public static bool UpdateRP(List<robot_points> lstrobot_p)
        {
            return RobotPointsDAL.UpdateRobotPoint(lstrobot_p);
        }

        /// <summary>
        /// 获取点位信息
        /// </summary>
        /// <param name="sPoName"></param>
        /// <returns></returns>
        public static robot_points GetPoint(string sPoName)
        {
            try
            {
                List<robot_points> lstrobot_p = QureyRP(sPoName);
                if (lstrobot_p.Count < 1)
                {
                    LogHelper.Debug($"获取点位信息{sPoName}失败，当前点位不存在");
                    return new robot_points();
                }
                else
                {
                    LogHelper.Debug($"获取点位信息{sPoName}成功");
                    return lstrobot_p[0];
                }
            }
            catch(Exception ex)
            {
                LogHelper.Error($"获取点位信息{sPoName}异常", ex);
                return new robot_points();
            }
        }
        /// <summary>
        /// 保存当前点位
        /// </summary>
        /// <param name="sPoName">点位名称</param>
        /// <param name="poCurrent">需要保存的点位</param>
        /// <param name="sToolName">工具坐标系名称</param>
        /// <param name="sToolValue">工具坐标系值</param>
        public static bool SavePoint(string sPoName, PointXYZ poCurrent, string sToolName, string sToolValue)
        {
            try
            {
                List<robot_points> lstrobot_p = QureyRP(sPoName);
                if (lstrobot_p.Count < 1)
                {
                    LogHelper.Debug($"当前点位{sPoName}不存在,保存点位信息{sPoName}失败");
                    return false;
                }
                robot_points robot_p = lstrobot_p[0];
                robot_p.PoValue = PointToStr(poCurrent);
                robot_p.ToolName = sToolName;
                robot_p.ToolValue = sToolValue;
                robot_p.ModifyTime = DateTime.Now;
                if (UpdateRP(robot_p))
                {
                    LogHelper.Debug($"保存点位信息{sPoName}成功");
                    return true;
                }
                else
                {
                    LogHelper.Debug($"保存点位信息{sPoName}失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"保存点位信息{sPoName}异常",ex);
                return false;
            }          
        }

        public static PointXYZ StrToPoint(string p)
        {
            PointXYZ np = new PointXYZ();
            if (!p.Contains(","))
                return np;
            string[] ss = p.Split(',');
            if (ss.Length == 6)
            {
                np.X = Utility.str2Double(ss[0]);
                np.Y = Utility.str2Double(ss[1]);
                np.Z = Utility.str2Double(ss[2]);
                np.A = Utility.str2Double(ss[3]);
                np.B = Utility.str2Double(ss[4]);
                np.C = Utility.str2Double(ss[5]);
            }
            return np;
        }
        public static string PointToStr(PointXYZ pointXYZ)
        {
            string sPoint = string.Empty;
            sPoint = pointXYZ.X.ToString() + "," + pointXYZ.Y.ToString() + "," + pointXYZ.Z.ToString() + ","
                + pointXYZ.A.ToString() + "," + pointXYZ.B.ToString() + "," + pointXYZ.C.ToString();
            return sPoint;
        }
    }
}